//
//  BGFiveCell.m
//  bigMall
//
//  Created by ios on 16/9/18.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGFiveCell.h"
#import "BGId_ValueModel.h"
#import "BGReadStepTwoModel.h"
@interface BGFiveCell () <UIPickerViewDelegate,UIPickerViewDataSource> {
    
    UIScrollView *wancheng;
    UITextField *textFiled;
    UILabel *titleLab;
    NSMutableArray *_dataArr;
}
@property (nonatomic,strong) UIPickerView *picker;
@end
@implementation BGFiveCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier dataArr:(NSMutableArray *)marr default_value:(NSArray *)default_valueArr{
    
    _dataArr = [NSMutableArray array];
    for (BGReadStepTwoModelArrDetail *model in marr) {
        [_dataArr addObject:model.value];
    }
    
    BGReadStepTwoModelArrDetail *model = default_valueArr.firstObject;
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        //修饰线
        UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1)];
        v1.backgroundColor = dockwhite_;
        [self.contentView addSubview:v1];
        
        titleLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 10, 100, 21)];
        titleLab.font = [UIFont systemFontOfSize:16];
        titleLab.text = self.titleText;
        CGSize labSize = [titleLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleLab.font,NSFontAttributeName, nil]];
        titleLab.height = labSize.height;
        titleLab.width = labSize.width;
        [self.contentView addSubview:titleLab];
        
        textFiled = [[UITextField alloc] initWithFrame:CGRectMake(titleLab.right + 10, 10, 100, 21)];
        textFiled.placeholder = @"请选择";
    
        if (model.value != nil) {
            textFiled.text = model.value;
        }
        
        textFiled.font = [UIFont systemFontOfSize:16];
        textFiled.userInteractionEnabled = NO;
        textFiled.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:textFiled];
        //picker
        self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, [UIApplication sharedApplication].keyWindow.bottom - [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 3)];
        
        self.picker.delegate = self;
        self.picker.dataSource = self;
        self.picker.backgroundColor = [UIColor whiteColor];
        self.picker.hidden = YES;
        self.picker.tag = 1;
        [self.picker selectRow:0 inComponent:0 animated:YES];
        [[UIApplication sharedApplication].keyWindow addSubview:self.picker];
        [self.picker showsSelectionIndicator];
        
        [self.picker reloadAllComponents];
        //创建确定的按钮
        wancheng = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.picker.y - 30, [UIScreen mainScreen].bounds.size.width, 30)];
        wancheng.backgroundColor = [UIColor whiteColor];
        [[UIApplication sharedApplication].keyWindow addSubview:wancheng];
        
        UIView *a = [[UIView alloc] initWithFrame:CGRectMake(0, 29, [UIScreen mainScreen].bounds.size.width, 1)];
        a.backgroundColor = [UIColor lightGrayColor];
        [wancheng addSubview:a];
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(wancheng.width - [UIScreen mainScreen].bounds.size.width / 4.3, 0, [UIScreen mainScreen].bounds.size.width / 4.3, 30)];
        [btn setTitle:@"完成" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        btn .titleLabel.textAlignment = NSTextAlignmentRight;
        [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        wancheng.hidden = YES;
        [wancheng addSubview:btn];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickera)];
        
        [self addGestureRecognizer:tap];
        
    }
    return self;
}

- (void)setTitleText:(NSString *)titleText {
    titleLab.text = [NSString stringWithFormat:@"%@：",titleText];
}
- (void)layoutSubviews {
    
    //设置标题的大小
    CGSize biaotiSize = [titleLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleLab.font,NSFontAttributeName, nil]];
    titleLab.width = biaotiSize.width;
    titleLab.height = biaotiSize.height;
    textFiled.frame = CGRectMake(titleLab.right + 10, 10, 100, 21);
    
}
- (void)pickera {
    
    textFiled.text = _dataArr[0];
    wancheng.hidden = NO;
    self.picker.hidden = NO;
    
}
- (void)btnClick {
    
    wancheng.hidden = YES;
    self.picker.hidden = YES;
    
}
#pragma mark pickerDatasourece
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 1;
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return _dataArr.count;
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    
    return _dataArr[row];
    
    
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.minimumFontSize = 8;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:18]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    
    textFiled.text = _dataArr[row];
    
    if ([_delegate respondsToSelector:@selector(pickerValueChange:index:)]) {
        [_delegate pickerValueChange:_dataArr[row] index:self.attr_id];
    }
    
}


@end
